package com.asion.qkan.ui.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.os.Handler;
import android.view.View;

import com.asion.qkan.R;

/**
 * Created by asion on 16-10-14.
 */

public class MusicPlayDiscView extends View {

    Paint paint;

    private Handler handler;
    // 光盘图片
    Bitmap bitmapDisc = BitmapFactory.decodeResource(getResources(),
            R.drawable.a9x);
    // 专辑图片
    Bitmap bitmapImage = BitmapFactory.decodeResource(getResources(),
            R.drawable.holder);
    Bitmap bitmapDiscCircular,bitmapCircularAblum;
    // 光盘指针图片
    Bitmap bitmapNeedle = BitmapFactory.decodeResource(getResources(),
            R.drawable.aag);

    public void setBitmapImage(Bitmap bitmapImage1) {
        bitmapCircularAblum = getCircularBitmap(bitmapImage1, 300);
        bitmapDiscCircular = getCircularBitmap(bitmapDisc,
                bitmapDisc.getWidth());

        postInvalidate();
        paint = new Paint();
        paint.setAntiAlias(true);
        handler = new Handler();
        handler.post(runnable);
    }

    public MusicPlayDiscView(Context context) {
        super(context);
        //分别获得光盘和专辑的圆形图片
        bitmapCircularAblum = getCircularBitmap(bitmapImage, 300);
        bitmapDiscCircular = getCircularBitmap(bitmapDisc,
                bitmapDisc.getWidth());

        paint = new Paint();
        paint.setAntiAlias(true);
        handler = new Handler();
        handler.post(runnable);
    }

    /**
     * 利用线程不断更新界面
     */
    private Runnable runnable = new Runnable() {
        public void run() {
            postInvalidate();
            handler.postDelayed(runnable, 50);
        }
    };

    //状态标志：
    int before = 0;
    //角度标志
    private int degreeFlag = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * 先画光盘与专辑图片
         */

        if (Constant.CurrentState == Constant.Play) {
            Constant.Degree ++;
            if (Constant.Degree > 360) {
                Constant.Degree = 0;
            }

            degreeFlag = Constant.Degree;

            canvas.save();
            //360为屏幕的中间位置，手机是720的宽度
            canvas.rotate(Constant.Degree, getWidth()/2,
                    getHeight()/2-70);
            canvas.drawBitmap(bitmapCircularAblum,
                    getWidth()/2 - bitmapCircularAblum.getWidth() / 2, getHeight()/2-bitmapCircularAblum.getHeight()/2-70, paint);
            canvas.drawBitmap(bitmapDisc,
                    getWidth()/2 - bitmapDiscCircular.getWidth() / 2, getHeight()/2-bitmapDiscCircular.getHeight()/2-70, paint);

            canvas.restore();

        } else {
            //before = 0;

            canvas.save();
            canvas.rotate(degreeFlag, getWidth()/2,
                    getHeight()/2-70);
            canvas.drawBitmap(bitmapCircularAblum,
                    getWidth()/2  - bitmapCircularAblum.getWidth() / 2, getHeight()/2-bitmapCircularAblum.getHeight()/2-70, paint);

            canvas.drawBitmap(bitmapDisc,
                    getWidth()/2  - bitmapDiscCircular.getWidth() / 2, getHeight()/2-bitmapDiscCircular.getHeight()/2-70, paint);

            canvas.restore();

        }

        /**
         * 再画光盘指针图片,三张图不能同时画
         */
        if (Constant.CurrentState == Constant.Play ) {
            canvas.drawBitmap(bitmapNeedle, getWidth()/2 - bitmapNeedle.getWidth() / 2+20,
                   -25, paint);
        } else {
            canvas.save();
            Matrix matrix = new Matrix();
            matrix.postRotate(-30);
            paint.setAntiAlias(true);
            //获得指针旋转后的图片
            Bitmap bm = Bitmap.createBitmap(bitmapNeedle, 0, 0,
                    bitmapNeedle.getWidth(), bitmapNeedle.getHeight(), matrix,
                    true);
            canvas.drawBitmap(bm, getWidth()/2 - bitmapNeedle.getWidth() / 2 +20, -80,
                    paint);
            canvas.restore();
        }

    }

    /**
     * 获得圆形图片的方法
     *
     */
    private Bitmap getCircularBitmap(Bitmap bitmap, int radius) {
        Bitmap sbmp = Bitmap.createScaledBitmap(bitmap, radius, radius, false);

        Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLACK);
        canvas.drawCircle(sbmp.getWidth() / 2, sbmp.getHeight() / 2,
                sbmp.getWidth() / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(sbmp, rect, rect, paint);
        return output;
    }

}
